package function;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class Delete implements Function{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入要删除的图书名称");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int index = -1;
        for (int i = 0; i < bookList.getOnNum(); i++) {
            if(bookList.getBook(i).getName().equals(name)) {
                index = i;
            }
        }
        if(index==-1) {
            System.out.println("没有该书籍可以删除");
        } else {
            while (index+1<bookList.getOnNum()) {
                Book book = bookList.getBook(index+1);
                bookList.setBook(index,book);
                index++;
            }
            bookList.setOnNum(bookList.getOnNum()-1);
            bookList.setBook(index,null);
            System.out.println("删除成功");

        }
    }
}
